import { ViewUtil } from "db://app-game/core/utils/ViewUtil";
import { ecs } from "db://app-game/libs/ecs/ECS";
import { GamePrefab } from "../../../common/config/GamePrefab";
import { JoysitickLoadParamsType } from "./Joysitick.type";
import { JoysitickControlComp } from "./model/JoysitickControlComp";
import { JoysitickModelComp } from "./model/JoysitickModelComp";
import { JoysitickManagerComp } from "./view/JoystickManagerComp";

/** 摇杆实例 */
@ecs.register("Joysitick")
export class Joysitick extends ecs.Entity {
  // 数据层
  JoysitickModel!: JoysitickModelComp;

  // 业务层
  JoysitickControl!: JoysitickControlComp;

  // 视图层
  JoysitickManager!: JoysitickManagerComp;
  init() {
    this.add(JoysitickModelComp);
    this.add(JoysitickControlComp);
  }

  load(res: JoysitickLoadParamsType) {
    const { id, parent, width, height, absolutePosition } = res;
    // 1.设置数据层参数
    this.JoysitickModel.id = id;
    this.JoysitickModel.width = width;
    this.JoysitickModel.height = height;
    this.JoysitickModel.absolutePosition = absolutePosition;

    // 2.加载视图
    let node = ViewUtil.createPrefabNode(GamePrefab.Joysitick);
    let mv = node.getComponent(JoysitickManagerComp)!;
    this.add(mv);
    node.parent = parent;

    // 3.设置冷却时间
    this.JoysitickControl.coolingTime = 0;
  }

  /** 刷新摇杆 */
  refresh(id: number) {
    this.JoysitickModel.id = id;
    this.JoysitickManager.refreshSvg();
  }

  /** 显示隐藏判断 */
  onVisibleAndHidden(event: string, res: { joysitick: []; skillIcon: [] }) {
    const isHide =
      res.joysitick.findIndex((v) => v === this.JoysitickModel.id) > -1;
    this.JoysitickManager.node.active = !isHide;
  }
}
